<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/21
 * Time: 10:56
 */

namespace app\api\controller\v2;



use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\model\SpecialColumn;
use app\api\model\SpecialContent;
use app\api\validate\IdMustInt;

class SpecialController extends BaseController
{
    //通过ID获取专题栏目
    public function getSpecialColumn()
    {
        $result = SpecialColumn::limit(2)->select();
        foreach($result as $k=>$v){
            if (!$v['pic']){
                $v->data('pic','http://'.$_SERVER['SERVER_NAME'].'/uploads/special_column/moren.png');
            }
        }
        $this -> doBack($result);
    }
    
    //通过专题栏目获取文章标题
    public function getSpecialTitle($id)
    {
        (new IdMustInt())->goCheck();
        $model = SpecialColumn::all();
        if (empty($model)){
            throw new MissException([
               'msg' => '没有专题栏目',
            ]);
        }
        $contentList = SpecialContent::where('cid','=',$id)->select();
        if (empty($contentList)){
            throw new MissException([
               'msg' => '此专题下没有文章',
            ]);
        }
        foreach($contentList as $k=>$v){
            if (!$v['pic']){
                $v->data('pic','http://'.$_SERVER['SERVER_NAME'].'/uploads/special_content/moren.png');
            }
        }
        collection($contentList)->visible(['id','description','keyword','pic','create_time']);
        throw new SuccessMessage([
            'data' => $contentList,
        ]);
    }
    
    //通过文章ID获取文章内容
    public function getContentByID($id)
    {
        (new IdMustInt())->goCheck();
        $result = SpecialContent::get($id);
        $this->doBack($result);
    }
}